package com.woniu.adopt.web;

import com.github.pagehelper.PageInfo;
import com.woniu.adopt.entity.Permission;
import com.woniu.adopt.result.ResultResponse;
import com.woniu.adopt.service.PermissionService;
import com.woniu.adopt.vo.PermissionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("permission")
@Api(description = "权限管理接口")
public class PermissionController {

    @Autowired
    PermissionService permissionService;

    @ApiOperation(value = "根据id显示权限菜单")
    @GetMapping("/showMenuByManager/{id}")
    public ResultResponse<List<PermissionVo>> showMenuByManager(@PathVariable("id") int mid) {
        ResultResponse<List<PermissionVo>> result = null;

        try {
            if (mid == 1) {
                List<PermissionVo> list = permissionService.queryAllMenu();
                result = new ResultResponse<>(200, "查询成功", list);
            } else {
                List<PermissionVo> list = permissionService.queryMenuByManager(mid);
                result = new ResultResponse<>(200, "查询成功", list);
            }

        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询失败");
        }
        return result;
    }

    @ApiOperation(value = "显示所有权限功能")
    @GetMapping("/showAllMenu")
    public ResultResponse<List<PermissionVo>> showAllMenu() {
        ResultResponse<List<PermissionVo>> result = null;
        try {
            List<PermissionVo> list = permissionService.queryAllMenu();
            result = new ResultResponse<>(200, "查询成功", list);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询失败");
        }
        return result;
    }

    @ApiOperation(value = "分页显示所有权限功能")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "now", value = "查询的当前页", required = true, defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "size", value = "每页显示多少条", required = true, defaultValue = "7"),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "name", value = "权限名称", required = false)
    })
    @GetMapping("/list")
    public ResultResponse<PageInfo<Permission>> list(
            @RequestParam(value = "now", defaultValue = "1") Integer now,
            @RequestParam(value = "size", defaultValue = "7") Integer size,
            String name) {
        ResultResponse<PageInfo<Permission>> result = null;
        try {
            PageInfo<Permission> pageInfo = permissionService.queryPermissionByPage(now, size, name);
            result = new ResultResponse<>(200, "查询成功!", pageInfo);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询失败!");
        }

        return result;
    }

    @ApiOperation(value = "查询父菜单权限功能")
    @GetMapping("/parentPer")
    public ResultResponse<List<Permission>> getParentPermission() {
        ResultResponse<List<Permission>> result = null;

        try {
            List<Permission> list = permissionService.queryParentMenu();
            result = new ResultResponse<>(200, "查询父菜单成功!", list);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询父菜单失败失败!");
        }
        return result;
    }

    @ApiOperation(value = "添加权限功能")
    @PostMapping("/add")
    public ResultResponse<Void> add(@RequestBody Permission permission) {
        ResultResponse<Void> result = null;

        try {
            permission.setUpdateTime(new Date());
            permission.setInsertTime(new Date());
            permissionService.add(permission);
            result = new ResultResponse<>(200, "添加权限成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "添加权限失败!");
        }
        return result;
    }

    @ApiOperation(value = "修改权限功能")
    @PutMapping("/modify")
    public ResultResponse<Void> modify(@RequestBody Permission permission) {
        ResultResponse<Void> result = null;

        try {
            //根据 ID 查询出来 当前数据  把前台传递过来的数据  赋值到新查询出来的对象上
            Permission p = permissionService.queryPerByKey(permission.getId());
            p.setName(permission.getName());
            p.setParentId(permission.getParentId());
            p.setUrl(permission.getUrl());
            p.setUrlType(permission.getUrlType());
            p.setUpdateTime(new Date());
            permissionService.modify(p);
            result = new ResultResponse<>(200, "修改权限成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "修改权限失败!");
        }
        return result;
    }

    @ApiOperation(value = "删除权限功能")
    @DeleteMapping("/del/{id}")
    public ResultResponse<Void> del(@PathVariable("id") int id) {
        ResultResponse<Void> result = null;

        try {
            permissionService.removePermissionByKey(id);
            result = new ResultResponse<>(200, "删除权限成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "删除权限失败!");
        }
        return result;
    }

    @ApiOperation(value = "根据id查询拥有的所有权限")
    @GetMapping("/getPermissionId/{id}")
    public ResultResponse<List<Integer>> getPermissionId(@PathVariable("id") int id) {
        ResultResponse<List<Integer>> result = null;

        try {
            List<Integer> list = permissionService.queryPermissionIdByRoleId(id);
            result = new ResultResponse<>(200, "查询成功!", list);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询失败!");
        }
        return result;
    }
}
